package com.huc.rest.init;

import com.alibaba.fastjson.JSONObject;
import com.huc.rest.utils.ContextUtils;
import com.huc.rest.vo.ServiceCoreVO;

import javax.lang.model.element.Element;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 利用Java反射机制
 */
public class BaseAction  {

    protected ServiceCoreVO serviceCoreVO = null;
    protected String action = "";
    protected String operatelogno = "";
    protected String recordno = "";
    protected String reqip = "";
    protected String trackid = "";
    protected String devicePort = "pc";
    protected int loginTimes = 0;
    protected String desKey="";

    protected String userno = "";
    protected String accessId = "";
    protected String accessKey = "";
    protected Long userId = 0l;
    protected String sinaUId  ="";
    protected String clientVersion = "";
    protected boolean paypassSetFlg = false;

    protected HttpServletRequest request = null;
    protected HttpServletResponse response = null;

    protected Object reqobject = null;
    protected Map<String, Object> resobject = null;

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public ServiceCoreVO execute(ServiceCoreVO serviceCoreVO) {
        action = serviceCoreVO.getAction();
        operatelogno = serviceCoreVO.getOperateLogno();
        try {
            Object executeAction = ContextUtils.getBean(serviceCoreVO.getBean());
            Class classObject =executeAction.getClass();
            Method method0 = classObject.getSuperclass().getDeclaredMethod("init",ServiceCoreVO.class);
            method0.invoke(executeAction,serviceCoreVO);
            Method method = classObject.getDeclaredMethod(serviceCoreVO.getMethod());
            return (ServiceCoreVO)method.invoke(executeAction);
        }catch (Exception e) {
            e.printStackTrace();
//            if(e.getCause()!=null&&e.getCause().getMessage()!=null){
//                String tp = e.getCause().getMessage();
//                if(ValidatorUtils.isDigital(tp)){
//                    int code = Integer.valueOf(tp);
//                    if(code>=200000){
//                        LogUtils.printLogInfo(CommonUtils.getTraceInfo(e.getCause())+StringUtils.getInstance().nullToStrTrim(tp));
//                    }else{
//                        LogUtils.printLogError(CommonUtils.getTraceInfo(e.getCause())+StringUtils.getInstance().nullToStrTrim(tp));
//                    }
//                }
//                throw new EduException(tp);
//            }
//            if(e.getMessage()!=null){
//                if(ValidatorUtils.isDigital(e.getMessage())){
//                    int code = Integer.valueOf(e.getMessage());
//                    if(code>=200000){
//                        LogUtils.printLogInfo(CommonUtils.getTraceInfo()+StringUtils.getInstance().nullToStrTrim(e.getMessage()));
//                    }else{
//                        LogUtils.printLogError(CommonUtils.getTraceInfo()+StringUtils.getInstance().nullToStrTrim(e.getMessage()));
//                    }
//                }
//            }
//            throw new EduException(e.getMessage());
        }
        return null;
    }

    protected String getRequestContentValue(String name) {
        return getRequestContentValue(this.reqobject, name);
    }

    public static String getRequestContentValue(Object content, String name) {
        if(content == null) {
            return "err";
        }
        String value = JSONObject.parseObject(JSONObject.toJSONString(content)).getString(name);
        return value.trim();
    }

    public void init(ServiceCoreVO serviceCoreVO){
        this.reqobject = serviceCoreVO.getReqObject();
        this.request = serviceCoreVO.getRequest();
        this.response = serviceCoreVO.getResponse();
        resobject = new LinkedHashMap<String, Object>();
        this.serviceCoreVO = serviceCoreVO;
        this.reqip = serviceCoreVO.getIp();
        this.trackid = serviceCoreVO.getTrackid();
        this.devicePort = serviceCoreVO.getDevicePort();
        this.loginTimes = serviceCoreVO.getLoginTimes();
        this.userId = serviceCoreVO.getUserId();
        this.desKey = serviceCoreVO.getDesKey();
        this.sinaUId = serviceCoreVO.getSinaUId();
        this.clientVersion = serviceCoreVO.getClientVersion();
        this.paypassSetFlg = serviceCoreVO.isPaypassSetFlg();
        this.accessId = serviceCoreVO.getAccessId();
    }

    protected ServiceCoreVO response() {
        if(resobject == null || resobject.isEmpty()) {
            return doResponse(100000);
        }
        return doResponse(resobject);
    }

    private ServiceCoreVO doResponse(Object object) {
//        if(ValidatorUtils.isNotEmpty(recordno)) {
//            serviceCoreVO.setRecordNo(recordno);
//        }
        serviceCoreVO.setResObject(object);
        return serviceCoreVO;
    }

    protected ServiceCoreVO response(int code) {
        return doResponse(code);
    }

    protected ServiceCoreVO response(String code) {

//        if(ValidatorUtils.isEmpty(code)) {
//            code = String.valueOf(100002);
//        }
        return doResponse(code);
    }

}
